<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//文章详情表
class Video_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->video = 'video';
        $this->video_comment = 'video_comment';
        $this->video_like = 'video_like';
        $this->video_favorite = 'video_favorite';
        $this->users_info = 'users_info';
    }
    function info($where){
        $this->db->select($this->bbs_article.'.*,'.
                $this->bbs_article_category.'.name cat_name,'.
                $this->users_info.'.nickname,'.
                $this->users_info.'.head_img');
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_article.'.authorid',left);
        $this->db->where($where);
        return $this->db->get()->row_array();
    }
    //获取文章阅读数量
    function get_article_read_num($where){
        $this->db->select('id,readnum,status,authorid,title');
        $this->db->from($this->bbs_article);
        $this->db->where($where);
        return $this->db->get()->row_array();
    }
    
    //获取用户所有文章阅读总量
    function get_article_read_num_total($where){
        $this->db->select('SUM(readnum) total');
        $this->db->from($this->bbs_article);
        $this->db->where($where);
        $result = $this->db->get()->row_array();
        return $result['total'];
    }
    
    function hot(){
        $this->db->select($this->bbs_article.'.*,'.$this->bbs_article_category.'.name');
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->where('hot',1);
        $this->db->where('status',1);
        $this->db->order_by($this->bbs_article.'.create_time','desc');
        $this->db->limit(3,0);
        return $this->db->get()->result_array();
    }
    //获取推荐文章分类
    function get_category_recommend($where_in){
        
        $this->db->select($this->bbs_article_category.'.*');
        $this->db->from($this->bbs_article_category);
        $this->db->where_in($this->bbs_article_category.".id",$where_in);
        $this->db->order_by('sort','asc');
        return $this->db->get()->result_array();
        
    }
    
    function get_article_award_list($where,$page,$limit){
        $this->db->select($this->bbs_reward.".*,".$this->users_info.'.head_img');
        $this->db->from($this->bbs_reward);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_reward.'.user_id',"left");
        $this->db->where($where);
        $this->db->order_by($this->bbs_reward.'.create_time','desc');
        $this->db->group_by($this->bbs_reward.'.user_id');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    function reward($article_id){
        $this->db->select($this->users_info.'.head_img');
        $this->db->from($this->bbs_reward);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_reward.'.user_id',left);
        $this->db->where($this->bbs_reward.'.article_id',$article_id);
        $this->db->order_by($this->bbs_reward.'.create_time','desc');
        $this->db->limit(8,0);
        return $this->db->get()->result_array();
    }
    
    function get_article_comment($where){
        $this->db->select(
                $this->bbs_article_comment.'.*');
        $this->db->from($this->bbs_article_comment);
        $this->db->where($where);
        $this->db->order_by($this->bbs_article_comment.'.create_time','asc');
        return $this->db->get()->row_array();
    }
    
    function get_article_comment_count($where){
        $this->db->select('*');
        $this->db->from($this->bbs_article_comment);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    
    function get_article_comment_list($where,$page,$limit){
        $this->db->select(
                $this->bbs_article_comment.'.*,'.'u1.nickname,'.'u1.head_img,'.'u1.signature,u2.nickname repay_nickname,'.
                $this->bbs_account.'.level,'.
                $this->bbs_account.'.reward,'.
                $this->bbs_user.'.salt,'.
                $this->bbs_count.'.articles_count,'.
                $this->bbs_count.'.fans_count');
        $this->db->from($this->bbs_article_comment);
        $this->db->join($this->users_info." u1",'u1.user_id='.$this->bbs_article_comment.'.uid',left);
        $this->db->join($this->users_info." u2",'u2.user_id='.$this->bbs_article_comment.'.repay_user_id',left);
        $this->db->join($this->bbs_account,$this->bbs_account.'.user_id='.$this->bbs_article_comment.'.uid',left);
        $this->db->join($this->bbs_count,$this->bbs_count.'.user_id='.$this->bbs_article_comment.'.uid',left);
        $this->db->join($this->bbs_user,$this->bbs_user.'.user_id='.$this->bbs_article_comment.'.uid',left);
        
        $this->db->where($where);
        $this->db->order_by($this->bbs_article_comment.'.create_time','asc');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    
    
    
    //插入评论或回复
    function insert_comment($data){
        $this->db->insert($this->bbs_article_comment,$data);
        $result = $this->db->insert_id();
        if($this->db->affected_rows()==1){      //评论成功记录评论数
           $this->db->set('commentnum','commentnum+1',FALSE);
           $this->db->where('id',$data['article_id']);
           $this->db->update($this->bbs_article);
           //count表添加记录
           $this->db->set('reply_count','reply_count+1',FALSE);
           $this->db->where('id',$data['uid']);
           $this->db->update($this->bbs_count);
        }
        return $result;
    }
    
    //删除当个评论或回复
    function delete_comment_siggle($comment_id){
        $this->db->where("id",$comment_id)->update($this->bbs_article_comment,array("is_show"=>0));
        $this->db->where(array("operator_id"=>$comment_id,"event_type"=>"event_reply_comment"))->update("bbs_notice",array("is_show"=>0));
        $this->db->where(array("operator_id"=>$comment_id,"event_type"=>"event_reply_comment"))->update("bbs_user_behavior",array("is_show"=>0));
    }
    //删除多个评论或回复
    function delete_comment_more($comment_ids){
        $this->db->where_in("id",$comment_ids)->update($this->bbs_article_comment,array("is_show"=>0));
        $this->db->where("event_type = 'event_reply_comment' or event_type = 'event_comment_article'")->where_in("operator_id",$comment_ids)->update("bbs_notice",array("is_show"=>0));
        $this->db->where("event_type = 'event_reply_comment' or event_type = 'event_comment_article'")->where_in("operator_id",$comment_ids)->update("bbs_user_behavior",array("is_show"=>0));
    }

    //获取视频点赞单个用户记录状态
    function get_video_like_info($where){
        $this->db->select('*');
        $this->db->from($this->video_like);
        $this->db->where($where);
        return $this->db->get()->row_array();
    }
    //获取视频点赞单个用户记录状态
    function get_video_like_status($where){
        $this->db->select('status');
        $this->db->from($this->video_like);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    //获取视频收藏单个用户记录状态
    function get_video_favorite_status($where){
        $this->db->select('*');
        $this->db->from($this->video_favorite);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    
    //增加视频点击赞的数量
    function insert_video_like($data){
        $this->db->insert($this->video_like,$data);
        if($this->db->affected_rows()==1){
           $this->db->set('likes','likes+1',FALSE);
           $this->db->where('id',$data['video_id']);
           $this->db->update($this->video);
        }
        return $this->db->affected_rows();
    }

    function insert_video_hates($data){
        $this->db->insert($this->video_like,$data);
        if($this->db->affected_rows()==1){
            $this->db->set('hates','hates+1',FALSE);
            $this->db->where('id',$data['video_id']);
            $this->db->update($this->video);
        }
        return $this->db->affected_rows();
    }
    function insert_favorite_like($data){
        $this->db->insert($this->bbs_favorite,$data);
        if($this->db->affected_rows()==1){      //评论成功记录评论数
           $this->db->set('favor_count','favor_count + 1',FALSE);
           $this->db->where('user_id',$data['user_id']);
           $this->db->update($this->bbs_count);
        }
        return $this->db->affected_rows();
    }
    
    function delete_favorite_like($where){
        $this->db->where("user_id",$where['user_id'])->where("article_id",$where['article_id'])->delete($this->bbs_favorite);
        if($this->db->affected_rows()==1){
           $this->db->set('favor_count','favor_count - 1',FALSE);
           $this->db->where('user_id',$where['user_id']);
           $this->db->update($this->bbs_count);
        }
        return $this->db->affected_rows();
    }
    
    
    
    
    function category($pid){
        $this->db->select('id,name');
        $this->db->from($this->bbs_article_category);
        $this->db->where('pid',$pid);
        $this->db->where('is_show','0');
        $this->db->order_by('sort','asc');
        return $this->db->get()->result_array();
    }
    
    function send($data){
        $this->db->insert($this->bbs_letter,$data);
        return $this->db->affected_rows();
    }
    function checkfocus($where){
        $this->db->select("*");
        $this->db->from($this->bbs_focus);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    function focus($data){
        $this->db->insert($this->bbs_focus,$data);
        return $this->db->affected_rows();
    }
    function report($data){
        $this->db->insert($this->bbs_report,$data);
        return $this->db->affected_rows();
    }
}
